'use strict'
import {
  Model,
  DataTypes,
  InferAttributes,
  InferCreationAttributes,
  CreationOptional,
} from 'sequelize'
import dayjs from 'dayjs'
import { Models } from '../type'
import sequelize from './index'

class Role extends Model<InferAttributes<Role>, InferCreationAttributes<Role>> {
  declare id: CreationOptional<number>
  declare name: string
  declare durationMonths: number
  declare price: CreationOptional<number>
  declare rank: CreationOptional<number>
  declare desc: CreationOptional<string>
  declare createdAt: CreationOptional<Date>
  declare updatedAt: CreationOptional<Date>

  static associate(models: Models) {
    Role.hasMany(models.User, { as: 'users' })
    // Role.hasMany(models.Order, { as: 'orders' })
  }
}
Role.init(
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notNull: {
          msg: '角色名称必须填写。',
        },
        notEmpty: {
          msg: '角色名称不能为空。',
        },
        len: {
          args: [2, 45],
          msg: '角色名称长度必须在2~45个字符之间。',
        },
      },
    },
    durationMonths: {
      type: DataTypes.INTEGER,
      allowNull: false,
      validate: {
        notNull: {
          msg: '角色时长必须填写。',
        },
        notEmpty: {
          msg: '角色时长不能为空。',
        },
        isInt: {
          msg: '角色时长必须为整数',
        },
        isPositive(value: number) {
          if (value <= 0) {
            throw new Error('角色时长必须是正整数。')
          }
        },
      },
    },
    price: {
      type: DataTypes.DECIMAL,
      // allowNull: false,
      defaultValue: 0,
      validate: {
        // notNull: {
        //   msg: '角色价格必须填写。',
        // },
        notEmpty: {
          msg: '角色价格不能为空。',
        },
        isNumeric: {
          msg: '角色价格必须为数字',
        },
        isPositive(value: number) {
          if (value < 0) {
            throw new Error('角色价格必须是正数')
          }
        },
      },
    },
    rank: {
      type: DataTypes.INTEGER,
      // allowNull: false,
      defaultValue: 1,
      validate: {
        // notNull: {
        //   msg: '排序必须填写。',
        // },
        notEmpty: {
          msg: '排序不能为空。',
        },
        isInt: {
          msg: '排序必须为整数。',
        },
        isPositive(value: number) {
          if (value <= 0) {
            throw new Error('排序必须是正整数。')
          }
        },
      },
    },
    desc: {
      type: DataTypes.STRING,
      validate: {
        len: { args: [0, 255], msg: '描述信息不能超过255位字符。' },
      },
    },
    createdAt: {
      type: DataTypes.DATE,
      get() {
        return this.getDataValue('createdAt')
          ? dayjs(this.getDataValue('createdAt')).format('YYYY-MM-DD HH:mm:ss')
          : null
      },
    },
    updatedAt: {
      type: DataTypes.DATE,
      get() {
        return this.getDataValue('updatedAt')
          ? dayjs(this.getDataValue('updatedAt')).format('YYYY-MM-DD HH:mm:ss')
          : null
      },
    },
  },
  {
    sequelize,
    modelName: 'Role',
  },
)

export default Role
